package com.aim.questionnaire.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.session.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.aim.questionnaire.beans.HttpResponseEntity;
import com.aim.questionnaire.common.Constans;
import com.aim.questionnaire.dao.UserEntityMapper;
import com.aim.questionnaire.dao.entity.UserEntity;
import com.aim.questionnaire.service.UserService;
import com.github.pagehelper.PageInfo;


/**
 * Created by wln on 2018\8\9 0009.
 */
@RestController
@RequestMapping("/admin")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(UserController.class);
    
    @Autowired
    private UserService userService;

    @Autowired
    private UserEntityMapper userEntityMapper;
   
    /**
     * 用户登录
     * @param map
     * @return
     */
    @RequestMapping(value="/userLogin",method= RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity userLogin(@RequestBody UserEntity userEntity) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try {
          
            List<UserEntity> hasUser = userEntityMapper.selectUserInfo(userEntity);
            if(CollectionUtils.isEmpty(hasUser) ) {
            	httpResponseEntity.setCode(Constans.EXIST_CODE);
            	httpResponseEntity.setData(null);
            	httpResponseEntity.setMessage(Constans.LOGIN_USERNAME_PASSWORD_MESSAGE);
            }else {
            	httpResponseEntity.setCode(Constans.SUCCESS_CODE);
            	httpResponseEntity.setData(hasUser.get(0));
            	httpResponseEntity.setMessage(Constans.LOGIN_MESSAGE);
            }

        } catch (Exception e) {
            logger.info("userLogin 用户登录>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.EXIST_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }

    /**
     * 查询用户列表（模糊搜索）
     * @param map
     * @return
     */
    @RequestMapping(value = "/queryUserList",method = RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity queryUserList(@RequestBody Map<String,Object> map) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try {
//            Session session = SecurityUtils.getSubject().getSession();
//            Object object = session.getAttribute("user");
            PageInfo pageInfo = userService.queryUserList(map);
            if (pageInfo != null) {
                httpResponseEntity.setCode(Constans.SUCCESS_CODE);
                httpResponseEntity.setData(pageInfo);
            }else {
                httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
                httpResponseEntity.setMessage(Constans.LOGOUT_NO_MESSAGE);
            }
        } catch (Exception e) {
            logger.info("questionnaireService 查询用户列表（模糊搜索）>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.EXIST_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }
    /**
     * 创建用户的基本信息
     * @param map
     * @return
     */
    @RequestMapping(value = "/addUserInfo",method = RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity addUserInfo(@RequestBody Map<String,Object> map) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try {
//            Session session = SecurityUtils.getSubject().getSession();
//            Object object = session.getAttribute("user");
            int result = userService.addUserInfo(map);

                if(result == 3) {
                    httpResponseEntity.setCode(Constans.USER_USERNAME_CODE);
                    httpResponseEntity.setMessage(Constans.USER_USERNAME_MESSAGE);
                }else {
                    httpResponseEntity.setCode(Constans.SUCCESS_CODE);
                    httpResponseEntity.setMessage(Constans.ADD_MESSAGE);
                }
//                map.put("user",object.toString());
//                int result = userService.addUserInfo(map);
//                if(result == 3) {
//                    httpResponseEntity.setCode(Constans.USER_USERNAME_CODE);
//                    httpResponseEntity.setMessage(Constans.USER_USERNAME_MESSAGE);
//                }else {
//                    httpResponseEntity.setCode(Constans.SUCCESS_CODE);
//                    httpResponseEntity.setMessage(Constans.ADD_MESSAGE);
//                }

//            }else {
//                httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
//                httpResponseEntity.setMessage(Constans.LOGOUT_NO_MESSAGE);
//            }
        } catch (Exception e) {
            logger.info("addUserInfo 创建用户的基本信息>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.EXIST_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }

    /**
     * 编辑用户的基本信息
     * @param map
     * @return
     */
    @RequestMapping(value = "/modifyUserInfo",method = RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity modifyUserInfo(@RequestBody Map<String,Object> map) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try {
            Session session = SecurityUtils.getSubject().getSession();
            Object object = session.getAttribute("user");
            if (object != null) {
                map.put("user",object.toString());
                int result = userService.modifyUserInfo(map);
                if(result ==3) {
                    httpResponseEntity.setCode(Constans.USER_CODE);
                    httpResponseEntity.setMessage(Constans.USER_PASSWORD_MESSAGE);
                }else if(result == 4) {
                    httpResponseEntity.setCode(Constans.USER_USERNAME_CODE);
                    httpResponseEntity.setMessage(Constans.USER_USERNAME_MESSAGE);
                }else{
                    httpResponseEntity.setCode(Constans.SUCCESS_CODE);
                    httpResponseEntity.setMessage(Constans.UPDATE_MESSAGE);
                }
            }else {
                httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
                httpResponseEntity.setMessage(Constans.LOGOUT_NO_MESSAGE);
            }
        } catch (Exception e) {
            logger.info("modifyUserInfo 编辑用户的基本信息>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.EXIST_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }


    /**
     *  根据用户id查询用户基本信息
     * @param userEntity
     * @return
     */
    @RequestMapping(value = "/selectUserInfoById",method = RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity selectUserInfoById(@RequestBody UserEntity userEntity) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try {
            Session session = SecurityUtils.getSubject().getSession();
            Object object = session.getAttribute("user");
            if (object != null) {
                Map<String,Object> result = userService.selectUserInfoById(userEntity);
                httpResponseEntity.setCode(Constans.SUCCESS_CODE);
                httpResponseEntity.setData(result);
            }else {
                httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
                httpResponseEntity.setMessage(Constans.LOGOUT_NO_MESSAGE);
            }
        } catch (Exception e) {
            logger.info("modifyUserInfo 查询用户的基本信息>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.EXIST_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }



    /**
     * 修改用户状态
     * @param map
     * @return
     */
    @RequestMapping(value = "/modifyUserStatus",method = RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity modifyUserStatus(@RequestBody Map<String,Object> map) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try {
            Session session = SecurityUtils.getSubject().getSession();
            Object object = session.getAttribute("user");
            if (object != null) {
                map.put("sessionUserId",session.getAttribute("id"));
                int result = userService.modifyUserStatus(map);
                if(result == 0) {
                    httpResponseEntity.setCode(Constans.USER_STATUS_CODE);
                    httpResponseEntity.setMessage(Constans.USER_STATUS_MESSAGE);
                }else if(result == 3) {
                    httpResponseEntity.setCode(Constans.USER_UPDATE_CODE);
                    httpResponseEntity.setMessage(Constans.USER_UPDATE_MESSAGE);
                }else  {
                    httpResponseEntity.setCode(Constans.SUCCESS_CODE);
                    httpResponseEntity.setMessage(Constans.UPDATE_MESSAGE);
                }
            }else {
                httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
                httpResponseEntity.setMessage(Constans.LOGOUT_NO_MESSAGE);
            }
        } catch (Exception e) {
            logger.info("modifyUserStatus 修改用户状态>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.EXIST_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }
    /**
     *  删除用户信息
     * @param userEntity
     * @return
     */
    @RequestMapping(value = "/deleteUserInfoById",method = RequestMethod.POST, headers = "Accept=application/json")
    public HttpResponseEntity deteleUserInfoById(@RequestBody UserEntity userEntity) {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try {
            Session session = SecurityUtils.getSubject().getSession();
            Object object = session.getAttribute("user");
            if (object != null) {
                int result = userService.deteleUserInfoById(userEntity);
                if(result == 3) {
                    httpResponseEntity.setCode(Constans.USER_DELETE_CODE);
                    httpResponseEntity.setMessage(Constans.USER_DELETE_MESSAGE);
                }else {
                    httpResponseEntity.setCode(Constans.SUCCESS_CODE);
                    httpResponseEntity.setData(result);
                }
            }else {
                httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
                httpResponseEntity.setMessage(Constans.LOGOUT_NO_MESSAGE);
            }
        } catch (Exception e) {
            logger.info("deleteUserInfoById 删除用户信息>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.EXIST_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }


    /**
     * 用户没有权限
     * @return
     */
    @RequestMapping(value = "/error")
    public HttpResponseEntity logout() {
        HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
        try {
            httpResponseEntity.setCode(Constans.SUCCESS_CODE);
            httpResponseEntity.setMessage(Constans.LOGOUT_PERMISSION_MESSAGE);
        } catch (Exception e) {
            logger.info("error 用户没有权限>>>>>>>>>>>" + e.getLocalizedMessage());
            httpResponseEntity.setCode(Constans.EXIST_CODE);
            httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
        }
        return httpResponseEntity;
    }
}
